package com.glorysoft.wangnianxinpda.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.databinding.ActivitySettingBinding;
import com.glorysoft.wangnianxinpda.utils.MyUtils;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import java.security.PrivilegedAction;

import static com.glorysoft.wangnianxinpda.base.Constant.DEFAULT_TIME_OUT;
import static com.glorysoft.wangnianxinpda.base.Constant.FILE_URL;
import static com.glorysoft.wangnianxinpda.base.Constant.IMEI_ID;
import static com.glorysoft.wangnianxinpda.base.Constant.NET_URL;

public class SettingActivity extends FreeUI_BaseActivity implements View.OnClickListener {
    private int COUNTS = 5;// 点击次数
    private FreeUI_GeneralFragmentDialog freeuiGeneralfragmentdialog = null;
    private ActivitySettingBinding binding;
    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_setting);

        freeUI_titleView.setTitle("设置");

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initData() {
        binding.tvVersion.setText(FreeApi_Utils.getVerName());
        binding.tvIP.setText(NET_URL);
        binding.tvFileIP.setText(FILE_URL);
        binding.tvTimeOut.setText(DEFAULT_TIME_OUT + "s");
        binding.tvIMEI.setText(IMEI_ID);
        binding.rlIP.setOnClickListener(this);
        binding.rlTimeOut.setOnClickListener(this);
        binding.rlFileIP.setOnClickListener(this);
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设置");
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), binding.llMain);
    }

    @Override
    public void onClick(View v) {
        if (new MyUtils().isFastClick()){
            showToast("您点击的太快了", false);
        }
        int id = v.getId();
        if (id == R.id.rlIP){
            freeuiGeneralfragmentdialog = FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", NET_URL, new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    if (freeuiGeneralfragmentdialog != null){
                        freeuiGeneralfragmentdialog.dismiss();
                    }
                    NET_URL = s;
                    binding.tvIP.setText(NET_URL);
                    FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "IP_URL1", s, getApplicationContext());
                    showToast("保存成功", true);
                }
            });
            freeuiGeneralfragmentdialog.show(getSupportFragmentManager(), "URL_IP_DIALOG");
        }else if (id == R.id.rlTimeOut){
            freeuiGeneralfragmentdialog = FreeApi_DialogUtils.getScanDialog(this, "请输入超时时间", DEFAULT_TIME_OUT, new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    if (freeuiGeneralfragmentdialog != null){
                        freeuiGeneralfragmentdialog.dismiss();
                    }
                    DEFAULT_TIME_OUT = s;
                    binding.tvTimeOut.setText(DEFAULT_TIME_OUT + "s");
                    FreeApi_SharePreferencesUtils.setSharePre("TIME_OUT_FILE", "TIME_OUT2", s, getApplicationContext());
                    showToast("保存成功，请重启APP生效", true);
                    binding.rlTimeOut.setVisibility(View.GONE);
                }
            });
            freeuiGeneralfragmentdialog.show(getSupportFragmentManager(), "TIME_OUT_DIALOG");

        }else  if (id == R.id.rlFileIP){
            freeuiGeneralfragmentdialog = FreeApi_DialogUtils.getScanDialog(this, "请输入文件地址", FILE_URL, new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    if (freeuiGeneralfragmentdialog != null){
                        freeuiGeneralfragmentdialog.dismiss();
                    }
                    FILE_URL = s;
                    binding.tvFileIP.setText(FILE_URL);
                    FreeApi_SharePreferencesUtils.setSharePre("URL_FILE", "FILE_IP", s, getApplicationContext());
                    showToast("保存成功", true);
                }
            });
            freeuiGeneralfragmentdialog.show(getSupportFragmentManager(), "FILE_IP_DIALOG");
        }
    }
}